package com.vincent.guava.caches;

import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
import com.google.common.collect.Maps;

public class CacheRevolution {
	Integer value = null;
	Integer key = null;
	LinkedHashMap<Integer, Integer> cacheMap;
	private ConcurrentHashMap<Integer, Integer> conMap;

	@BeforeTest
	public void init() {
		cacheMap = Maps.newLinkedHashMap();
		conMap = new ConcurrentHashMap<Integer, Integer>();
	}

	public void linkedHashMap() {
		LinkedHashMap<Object, Object> map = Maps.newLinkedHashMap();
		map.put(key, value);
		map.containsKey(key);
		map.get(key);
		map.remove(key);
	}

	@Test(threadPoolSize = 10, invocationCount = 3000)
	public void testCon() {
		Integer integer = cacheMap.get(1);
		System.out.print(integer);
		cacheMap.put(1, 1);
		try {
			Thread.sleep(10);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		cacheMap.put(1, 2);
	}

	public static class MylinkedHashMap<T, R> extends LinkedHashMap<T, R> {
		private static final long serialVersionUID = 5351184497855962457L;
		private int maxCapacity = 100;

		@Override
		protected boolean removeEldestEntry(Entry<T, R> eldest) {
			return size() >= this.maxCapacity;
		}

	}

	public void concurrentHashMap() {
		conMap.put(key, value);
		conMap.containsKey(key);
		conMap.get(key);
		conMap.remove(key);
	}

	public void cacheBuilderTester(){
		Cache<String,Object> cache = CacheBuilder.newBuilder()
		.expireAfterAccess(15, TimeUnit.MINUTES)
		.expireAfterWrite(15, TimeUnit.MINUTES)
		.maximumSize(100)
		.maximumWeight(15)
		.removalListener(new RemovalListener<String,Object>(){
			@Override
			public void onRemoval(RemovalNotification<String,Object> notification) {
				// TODO Auto-generated method stub
				
			}})
		.build();
		
		cache.put(new String("Key"), new Object());
	}
}
